
(load-plugin-in "rust-mode")
;;(loadenv-path "ROOT" "src/rust/misc/emacs")

(require 'rust-mode)

;;按键绑定
(defun rust-run-project ()
  "run project"
  (interactive)
  (compile-project (concat "rust run " (buffer-name))) )

(defun rust-compile-debug ()
  "compile project"
  (interactive)
  (setq default-directory (file-name-directory buffer-file-name))
  (setq-default ext-name "")
  (if (string= (getenv "GOOS") "windows") (setq ext-name ".exe"))
  (let ((name (nth 0 (last (split-string (directory-file-name (file-name-directory buffer-file-name)) "/") 1) ))
	;;(name (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
	(suffix (concat "_" (getenv "GOOS") "_" (getenv "GOARCH") "_" "debug")))
    (compile-project (concat "rustc " (concat name  suffix ext-name))))) ;;"go build -gcflags \"-N -l\" -o "

(defun rust-compile-release ()
  "compile project"
  (interactive)
  (setq default-directory (file-name-directory buffer-file-name))
  (setq-default ext-name "")
  (if (string= (getenv "GOOS") "windows") (setq ext-name ".exe"))
  (let ((name (nth 0 (last (split-string (directory-file-name (file-name-directory buffer-file-name)) "/") 1) ))
	;;(name (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
	(suffix (concat "_" (getenv "GOOS") "_" (getenv "GOARCH"))))
    (compile-project (concat "rustc " (concat name  suffix ext-name)))))

(defun grust-clean-project ()
  "clean project"
  (interactive)
  (compile-project "go clean -i"))

(defun rust-default-settings ()
  (setq default-tab-width 4)
  (setq c-basic-offset 4)
  ;;(hs-minor-mode t)
  (setq tab-width 4 indent-tabs-mode nil)
  ;;(setq tab-width 4)
  (setq show-trailing-whitespace t)

  (define-key rust-mode-map [f6] 'rust-run-project)   ;;Ctrl+f7 编译工程-release
  (define-key rust-mode-map [f7] 'rust-compile-debug)   	;;F7 编译工程-debug
  (define-key rust-mode-map [C-f7] 'rust-compile-release)   ;;Ctrl+f7 编译工程-release
  (define-key rust-mode-map [S-f7] 'rust-clean-project)   	;;Shift+f7 清理工程
)

(add-hook 'rust-mode-hook 'rust-default-settings)

(add-hook
 'rust-mode-hook
 '(lambda ()

    ;; Menu bar
    (require 'easymenu)
    (defconst rust-hooked-menu
      '("Rust"
	;;["Run Buffer" go-run t]
	["Run Project" rust-run-project t]
	["--" nil nil]
	["Compile debug" rust-compile-debug t]
	["Compile release" rust-compile-release t]
	["Clean" rust-clean-project t]))
    (easy-menu-define
      rust-added-menu
      (current-local-map)
      "rust menu"
      rust-hooked-menu)))
;;--end--
(provide 'mod-rust)
